package com.aefottt.struggle_star.util;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class DisplayUtil {
    /**
     * dp转为px
     */
    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 隐藏软键盘
     */
    public static void hideSoftInput(Context context){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 根据isAchieved改变EditText内容字体样式
     */
    public static void changeEtContent(EditText et, String title, int isAchieved){
        if (isAchieved == 1) {
            // 已完成，设置删除线及灰色字体
            SpannableStringBuilder spanTitle = new SpannableStringBuilder(title);
            spanTitle.setSpan(new StrikethroughSpan(), 0, title.length(),
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            spanTitle.setSpan(new ForegroundColorSpan(Color.GRAY), 0, title.length(),
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            et.setText(spanTitle);
        } else {
            et.setText(title);
        }
    }
}
